/**
 *
 */
package co.fitstart.weapp.api.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo2do.core.entity.page.Page;
import com.demo2do.core.persistence.GenericDaoSupport;
import com.demo2do.core.persistence.annotation.Dao;

import co.fitstart.entity.content.Activity;
import co.fitstart.entity.content.Article;
import co.fitstart.entity.content.Module;
import co.fitstart.entity.content.News;
import co.fitstart.entity.content.Status;
import co.fitstart.entity.content.page.Webpage;
import co.fitstart.entity.utils.Filter;
import co.fitstart.weapp.api.dao.ContentDao;
import co.fitstart.weapp.api.dao.ContentDaoSupport;
import co.fitstart.weapp.api.service.ContentService;

/**
 * @author downpour
 *
 */
@Service("contentService")
public class ContentServiceImpl implements ContentService {

    @Autowired
    private GenericDaoSupport genericDaoSupport;

    @Autowired
    private ContentDaoSupport contentDaoSupport;
    
    @Dao
    private ContentDao contentDao;

    /* (non-Javadoc)
     * @see co.fitstart.tencent.service.ContentService#getWebPage(java.lang.String)
     */
    public Webpage getWebPage(String alias) {
        return contentDaoSupport.getWebpage(alias);
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.api.service.ContentService#getModule(java.lang.Long)
     */
    public Module getModule(Long moduleId) {
        return contentDao.loadModule(moduleId);
    }
    
    /* (non-Javadoc)
     * @see co.fitstart.api.service.ContentService#listModules()
     */
    public List<Module> listModules() {
        return contentDao.listModule();
    }

    /* (non-Javadoc)
     * @see co.fitstart.tencent.service.ContentService#listNews(com.demo2do.core.web.resolver.Page)
     */
    @SuppressWarnings("unchecked")
    public List<News> listNews(Page page) {
        String querySentence = "FROM News news WHERE news.disabled = :disabled ORDER BY news.serial DESC, news.createTime DESC";
        Map<String, Object> paramters = new HashMap<String, Object>(1);
        paramters.put("disabled", false);

        List<News> news = genericDaoSupport.searchForList(querySentence, paramters, page.getBeginIndex(), page.getMaxResultRecords());
        page.calculate(news.size());

        return news.size() >= page.getEveryPage() ? news.subList(0, page.getEveryPage()) : news;
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.service.ContentService#countActivity(java.lang.String)
     */
    public int countActivity(String keyword) {
        if (!StringUtils.isBlank(keyword) && !StringUtils.equalsIgnoreCase(keyword, "null")) {
            return contentDao.countActivity(Status.ONLINE, "%" + keyword + "%");
        } else {
            return contentDao.countActivity(Status.ONLINE);
        }
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.service.ContentService#listActivity(int)
     */
    public List<Activity> listActivity(int perPage) {
        return contentDao.listActivity(Status.ONLINE, 0, perPage);
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.service.ContentService#listActivity(java.lang.String, com.demo2do.core.entity.page.Page)
     */
    public List<Activity> listActivity(String keyword, Page page) {
        
        List<Activity> activities = new ArrayList<Activity>();

        // Add name like
        if (!StringUtils.isBlank(keyword) && !StringUtils.equalsIgnoreCase(keyword, "null")) {
            activities = contentDao.listActivity(Status.ONLINE, "%" + keyword + "%", page.getBeginIndex(), page.getEveryPage());
        } else {
            activities = contentDao.listActivity(Status.ONLINE, page.getBeginIndex(), page.getEveryPage());
        }

        page.calculate();

        return activities;
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.service.ContentService#countArticle(java.lang.Long, java.lang.String)
     */
    public int countArticle(Long moduleId, String keyword) {
        Filter filter = new Filter();
        // Add module equals
        if (moduleId != null) {
            filter.addEquals("modules", "module.id", moduleId);
        }
        // Add article name like
        if (!StringUtils.isBlank(keyword)) {
            filter.addLike("article", "name", keyword);
        }

        String querySentence = "SELECT COUNT(*) FROM Article AS article LEFT JOIN article.modules AS modules WHERE article.disabled = false " + filter.getClauseSentence() + "ORDER BY article.createTime DESC";

        int totals = genericDaoSupport.searchForInt(querySentence, filter.getParameters());

        return totals;
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.weapp.api.service.ContentService#listArticle(int)
     */
    public List<Article> listArticle(int perPage) {
        return contentDao.listArticle(0, perPage);
    }

    /* (non-Javadoc)
     * @see co.fitstart.tencent.service.ContentService#listArticle(java.lang.Long, java.lang.String, com.demo2do.core.web.resolver.Page)
     */
    @SuppressWarnings("unchecked")
    public List<Article> listArticle(Long moduleId, String keyword, Page page) {

        Filter filter = new Filter();
        // Add module equals
        if (moduleId != null) {
            filter.addEquals("modules", "module.id", moduleId);
        }
        // Add article name like
        if (!StringUtils.isBlank(keyword)) {
            filter.addLike("article", "name", keyword);
        }

        String querySentence = "SELECT article FROM Article AS article LEFT JOIN article.modules AS modules WHERE article.disabled = false " + filter.getClauseSentence() + "ORDER BY article.createTime DESC";

        List<Article> articles = genericDaoSupport.searchForList(querySentence, filter.getParameters(), page.getBeginIndex(), page.getEveryPage());
        page.calculate();

        return articles;
    }

    /* (non-Javadoc)
     * @see co.fitstart.tencent.service.ContentService#listArticle(co.fitstart.common.entity.content.page.Webpage)
     */
    public Map<String, List<Article>> listArticle(Webpage webpage) {

        String[] modules = webpage.getModuleAlias();

        if(ArrayUtils.isNotEmpty(modules)) {
            // construct result
            Map<String, List<Article>> result = new HashMap<String, List<Article>>();
            for(String moduleAlias : modules) {
                // get module first
                Module module = contentDaoSupport.getModule(moduleAlias);
                if(module != null) {
                    // get hot articles by module
                    List<Article> articles = contentDaoSupport.getHotArticleByModule(module);
                    result.put(moduleAlias, articles);
                }
            }
            return result;
        }
        return null;
    }

}
